package com.duo.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.duo.dto.ShopDto;
import com.duo.result.BaseResult;
import com.duo.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Xrx
 * @Description:
 * @CreateTime: 2023/7/8 19:19
 */
@RestController
@RequestMapping("/admin/shop")
@Slf4j
public class ShopController {

    private static final String STATUS_KEY = "SHOP:STATUS";

    private static final String SHOP_NAME_KEY = "SHOP:NAME";

    private static final String SHOP_IMAGE_KEY = "SHOP:IMAGE";

    private static final String SHOP_DESCRIPTION_KEY = "SHOP:DESCRIPTION";

    private static final String SHOP_PACKING_FEE = "SHOP:PACKING:FEE";

    @Autowired
    private RedisUtils redisUtils;


    /**
     * 保存店铺信息
     * @param shopDto
     * @return
     */
    @PostMapping("/save")
    public Object save(@RequestBody ShopDto shopDto) {
        log.info("保存店铺信息：{}", shopDto);
        redisUtils.setCacheObject(SHOP_NAME_KEY, shopDto.getShopName());
        redisUtils.setCacheObject(SHOP_IMAGE_KEY, shopDto.getImage());
        redisUtils.setCacheObject(SHOP_DESCRIPTION_KEY, shopDto.getDescription());
        return BaseResult.success("保存成功");
    }

    /**
     * 更新店铺信息
     */
    @PutMapping("/update")
    public Object update(@RequestBody ShopDto shopDto) {
        log.info("更新店铺信息：{}", shopDto);
        String shopName = shopDto.getShopName();
        String image = shopDto.getImage();
        String description = shopDto.getDescription();
        if (StrUtil.isNotBlank(shopName)) {
            redisUtils.setCacheObject(SHOP_NAME_KEY, shopName);
        }
        if (StrUtil.isNotBlank(image)) {
            redisUtils.setCacheObject(SHOP_IMAGE_KEY, image);
        }
        if (StrUtil.isNotBlank(description)) {
            redisUtils.setCacheObject(SHOP_DESCRIPTION_KEY, description);
        }
        return BaseResult.success("更新成功");
    }

    /**
     * 获取店铺信息
     * @return
     */
    @GetMapping("/get")
    public Object get() {
        Map<String, String> map = new HashMap<>();
        map.put("shopName", redisUtils.getCacheObject(SHOP_NAME_KEY));
        map.put("shopImage", redisUtils.getCacheObject(SHOP_IMAGE_KEY));
        map.put("shopDescription", redisUtils.getCacheObject(SHOP_DESCRIPTION_KEY));
        return BaseResult.success("获取成功", map);
    }

    /**
     * 设置店铺的营业状态
     * @param status
     * @return
     */
    @PutMapping("/setStatus/{status}")
    public Object setStatus(@PathVariable Integer status) {
        log.info("设置店铺的营业状态为：{}", status == 1 ? "营业中" : "打烊中");
        redisUtils.setCacheObject(STATUS_KEY, status);
        return BaseResult.success("设置成功");
    }

    /**
     * 获取店铺的营业状态
     * @return
     */
    @GetMapping("/getStatus")
    public Object getStatus(){
        Integer status = redisUtils.getCacheObject(STATUS_KEY);
        String statusMessage = status == 1 ? "营业中" : "打烊中";
        log.info("获取到店铺的营业状态为：{}", statusMessage);
        return BaseResult.success("获取成功", statusMessage);
    }

    /**
     * 设置打包费
     */
    @PutMapping("/setPackingFee/{packingFee}")
    public Object setPackingFee(@PathVariable Integer packingFee) {
        log.info("设置打包费为：{}", packingFee);
        redisUtils.setCacheObject(SHOP_PACKING_FEE, packingFee);
        return BaseResult.success("设置成功");
    }


}
